import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:flutterapp6/model/cartmodellist.dart';
// import 'package:flutterapp6/provied/cartmodel.dart';
import '../model/cart.dart';
import '../provied/cartmodel.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../model/cart.dart';
import 'dart:convert';
//import '../provied/cartmodel.dart';

class CartPage extends StatefulWidget {
  CartPage({
    Key key,
  }) : super(key: key);

  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  // Map arguments;
  // _CartPageState({this.arguments});
  List<Map> listgoods = [];
  List getgoods;
  @override
  void initState() {
   setState(() {
     context.read<CartProvide>().getcartinfo();
      //CartProvide().getcartinfo();
   });
    //  getgoods=Provider.of<CartProvide>(context).getgoods;
    super.initState();
    this._getcart();
    CartProvide().getcartinfo();
    //getcartinfo();
    print('购物车${goodslist}');
    list();
  }

  list() {
    //if(goodslist)
  }
  String goodsv = "[]";
  //bool foo=Provider.of<CartProvide>;
  List<Map> templist;
  void _getcart() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    goodsv = prefs.getString('cartinfo');
    var temp = goodsv == null ? [] : json.decode(goodsv.toString());
    templist = (temp as List).cast();

    //  print('购物车${templist[0]['check']}');
  }

  List<Map> a = [
    {"img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'},
    {"img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'},
    {"img": 'http://dbyxs.xyz:3005/uploads/fae4e814a5d9510d10277ff78ea6dc12'},
  ];
  //商品图片
//购物车商品
Widget _cartgoods(BuildContext context,int index){
   searchs();
        final ValueNotifier<List> _counter = ValueNotifier<List>([]);
        return ValueListenableBuilder(
            child: null,
            valueListenable: _counter,
            builder: (BuildContext context, value, Widget child) {
              return FutureBuilder(
                  // future:goodslist;
                  builder: (context, snapshot) {
                //  goodslist;
                return Card(
                  child: Container(
                      child: Row(
                    children: <Widget>[
                      Checkbox(
                          value: goodslist.length <= 0
                              ? false
                              : goodslist[index]['check'],
                          activeColor: Colors.pink,
                          onChanged: (bool val) {
                            setState(() {
                              CartProvide()
                                  .checkState(goodslist[index]['goodsId']);
                              //  this._getcart();
                              // searchs();
                              _cartlist();
                              // print('没有${Provider.of<CartProvide>(context).allPrice}');
                              print('购物${allprices}${allcount}');
                              // print('没有${Provider.of(context).goodslist}');
                            });
                          }),
                      Container(
                        width: 80.0,
                        padding: EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                            border:
                                Border.all(width: 1, color: Colors.black12)),
                        // child: Image.network(f,fit:BoxFit.cover),
                        child: Image.network(
                            '${goodslist.length <= 0 ? '' : goodslist[index]['images']}',
                            fit: BoxFit.cover),
                      ),
                      Container(
                        width: 120.0,
                        // height: 100,
                        margin: EdgeInsets.all(5),
                        // padding: EdgeInsets.all(10),
                        alignment: Alignment.topLeft,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                '${goodslist.length <= 0 ? '' : goodslist[index]['goodsname']}',
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                              // Container(
                              //   // width: 90,
                              //   // height: 30,
                              //   padding: EdgeInsets.all(5),
                              //   child:
                              // )
                              Row(
                                children: <Widget>[
                                  IconButton(
                                    icon: Icon(
                                      Icons.remove,
                                      size: 12,
                                    ),
                                    onPressed: () {
                                      setState(() {
                                        CartProvide().removecart(
                                            goodslist[index]['goodsId']);
                                      });
                                    },
                                  ),
                                  Container(
                                    width: 20,
                                    height: 20,
                                    child: Center(
                                        child: Text(
                                            '${goodslist.length <= 0 ? '' : goodslist[index]['count']}')),
                                    decoration: BoxDecoration(
                                        border: Border.all(
                                            width: 1, color: Colors.black12)),
                                  ),
                                  IconButton(
                                      icon: Icon(
                                        Icons.add,
                                        size: 12,
                                      ),
                                      onPressed: () {
                                        setState(() {
                                          CartProvide().addcart(
                                              goodslist[index]['goodsId']);
                                        });
                                      })
                                ],
                              ),
                            ]),
                      ),
                      Container(
                        width: 80,
                        height: 100,
                        alignment: Alignment.centerRight,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: <Widget>[
                              Text(
                                '￥${goodslist.length <= 0 ? '' : goodslist[index]['price']}元',
                                style:
                                    TextStyle(color: Colors.red, fontSize: 16),
                              ),
                              Container(
                                child: InkWell(
                                    onTap: () {
                                      setState(() {
                                        CartProvide().detailremove(
                                            goodslist[index]['goodsId']);
                                      });
                                      print('我被点击了删除');
                                    },
                                    child: Icon(
                                      Icons.delete_forever,
                                      color: Colors.red,
                                      size: 30,
                                    )),
                              )
                            ]),
                      )
                    ],
                  )),
                );
              });
            });
}
  //商品选中按钮
  List<Widget> _cartlist() => List.generate(goodslist.length, (index) {
        searchs();
        final ValueNotifier<List> _counter = ValueNotifier<List>([]);
        return ValueListenableBuilder(
            child: null,
            valueListenable: _counter,
            builder: (BuildContext context, value, Widget child) {
              return FutureBuilder(
                  // future:goodslist;
                  builder: (context, snapshot) {
                //  goodslist;
                return Card(
                  child: Container(
                      child: Row(
                    children: <Widget>[
                      Checkbox(
                          value: goodslist.length <= 0
                              ? false
                              : goodslist[index]['check'],
                          activeColor: Colors.pink,
                          onChanged: (bool val) {
                            setState(() {
                              CartProvide()
                                  .checkState(goodslist[index]['goodsId']);
                              //  this._getcart();
                              // searchs();
                              _cartlist();
                              // print('没有${Provider.of<CartProvide>(context).allPrice}');
                              print('购物${allprices}${allcount}');
                              // print('没有${Provider.of(context).goodslist}');
                            });
                          }),
                      Container(
                        width: 80.0,
                        padding: EdgeInsets.all(3.0),
                        decoration: BoxDecoration(
                            border:
                                Border.all(width: 1, color: Colors.black12)),
                        // child: Image.network(f,fit:BoxFit.cover),
                        child: Image.network(
                            '${goodslist.length <= 0 ? '' : goodslist[index]['images']}',
                            fit: BoxFit.cover),
                      ),
                      Container(
                        width: 120.0,
                        // height: 100,
                        margin: EdgeInsets.all(5),
                        // padding: EdgeInsets.all(10),
                        alignment: Alignment.topLeft,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                '${goodslist.length <= 0 ? '' : goodslist[index]['goodsname']}',
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                              ),
                              // Container(
                              //   // width: 90,
                              //   // height: 30,
                              //   padding: EdgeInsets.all(5),
                              //   child:
                              // )
                              Row(
                                children: <Widget>[
                                  IconButton(
                                    icon: Icon(
                                      Icons.remove,
                                      size: 12,
                                    ),
                                    onPressed: () {
                                      setState(() {
                                        CartProvide().removecart(
                                            goodslist[index]['goodsId']);
                                      });
                                    },
                                  ),
                                  Container(
                                    width: 20,
                                    height: 20,
                                    child: Center(
                                        child: Text(
                                            '${goodslist.length <= 0 ? '' : goodslist[index]['count']}')),
                                    decoration: BoxDecoration(
                                        border: Border.all(
                                            width: 1, color: Colors.black12)),
                                  ),
                                  IconButton(
                                      icon: Icon(
                                        Icons.add,
                                        size: 12,
                                      ),
                                      onPressed: () {
                                        setState(() {
                                          CartProvide().addcart(
                                              goodslist[index]['goodsId']);
                                        });
                                      })
                                ],
                              ),
                            ]),
                      ),
                      Container(
                        width: 80,
                        height: 100,
                        alignment: Alignment.centerRight,
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            children: <Widget>[
                              Text(
                                '￥${goodslist.length <= 0 ? '' : goodslist[index]['price']}元',
                                style:
                                    TextStyle(color: Colors.red, fontSize: 16),
                              ),
                              Container(
                                child: InkWell(
                                    onTap: () {
                                      setState(() {
                                        CartProvide().detailremove(
                                            goodslist[index]['goodsId']);
                                      });
                                      print('我被点击了删除');
                                    },
                                    child: Icon(
                                      Icons.delete_forever,
                                      color: Colors.red,
                                      size: 30,
                                    )),
                              )
                            ]),
                      )
                    ],
                  )),
                );
              });
            });
      });
  @override
  Widget build(
    BuildContext context,
  ) {
    searchs();
    final ValueNotifier<List> _counter = ValueNotifier<List>([]);
    // List getgoods=Provider.of<CartProvide>(context).getgoods;
    // print('我是购物车列表${getgoods}');
    int foo = Provider.of<CartProvide>(context).allGoodsCount;
    return
        // Scaffold(
        //   appBar: AppBar(
        //     title: Center(child: Text('购物车'),),
        //   ),
        //   body:
        ValueListenableBuilder(
            valueListenable: _counter,
            child: null,
            builder: (BuildContext context, value, Widget child) {
              return Scaffold(
                appBar: AppBar(
                  title: Center(
                    child: Text('购物车'),
                  ),
                ),
                body: Stack(
                  children: <Widget>[
                    Container(
                        // margin: EdgeInsets.only(top:10),
                        // height: double.infinity,
                        padding: EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            border: Border(
                                bottom: BorderSide(
                                    width: 1, color: Colors.black12))),
                        child: ListView(
                          children: goodslist == [] ? Text('加载中') : _cartlist()
                        )
                        // Column(
                        //   mainAxisAlignment: MainAxisAlignment.start,
                        //   // children: <Widget>[
                        //   //   Container(
                        //   //     width: ScreenUtil().setWidth(650),
                        //   //     child: ListView.builder(itemBuilder: _cartgoods,itemCount: goodslist.length),
                        //   //   )
                        //   // ],
                        //   children: goodslist == [] ? Text('加载中') : _cartlist()
                        // )
                        ),
                    Container(
                      // width: 350,
                      // height: 40,
                      // decoration: BoxDecoration(
                      // border: Border(top:BorderSide(width: 1,color:Colors.black12))
                      // ),
                      child: Positioned(
                          bottom: 0,
                          child: Container(
                            width: 355,
                            margin: EdgeInsets.all(10),
                            //padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                            decoration: BoxDecoration(
                                border: Border(
                                    top: BorderSide(
                                        width: 1, color: Colors.black12))),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Row(
                                  children: <Widget>[
                                    Text('全选'),
                                    Checkbox(
                                      value: allcheck,
                                      onChanged: (bool val) {
                                        setState(() {
                                          CartProvide().checkall();
                                        });
                                      },
                                      activeColor: Colors.red,
                                      hoverColor: Colors.red,
                                    ),
                                  ],
                                ),
                                Container(
                                  margin: EdgeInsets.fromLTRB(0, 0, 10, 0),
                                  child: Row(
                                    children: <Widget>[
                                      Text('合计:'),
                                      Text(
                                        '￥${allprices == null ? 0 : allprices}',
                                        style:
                                            TextStyle(color: Colors.redAccent),
                                      ),
                                      SizedBox(width: 10),
                                      RaisedButton(
                                          color: Colors.orange[300],
                                          child: Text(
                                              '结算(${allcount == null ? '' : allcount})'),
                                          onPressed: () {
                                            print(goodslist);
                                            //final v=context.watch<CartProvide>().user;
                                            //Navigator.pushNamed(context, '/login');
                                            // context.read<CartProvide>().goodsmodity();
                                           // print('没有用户${v}');
                                              if(counts){
                                                context.read<CartProvide>().goodsremove();
                                              Navigator.pushNamed(
                                                context, '/order',
                                                arguments: {"id": 0});
                                              }else{
                                                Navigator.pushNamed(context, '/login');
                                              }
                                             //context.read<CartProvide>().getOrder();
                                            //  CartProvide().checkall();
                                          }),
                                      //SizedBox(width:5)
                                    ],
                                  ),
                                )
                              ],
                            ),
                          )),
                    )
                  ],
                ),
              );
            });
    // );
  }
}
//  Column(
//          children:<Widget>[
//            Row(
//              mainAxisAlignment: MainAxisAlignment.spaceBetween,
//              children:<Widget>[
//                Text('购物车',style: TextStyle(fontSize: 26,fontWeight:FontWeight.w400 ),),
//                Container(
//                  child:GestureDetector(child: Text('管理'),
//                  onTap: (){
//                    print('我被点击了');
//                  },
//                  )
//                )

//              ]
//            )

//          ]
//        )
